//
//  XLWelcomeViewController.swift
//  SinaWeibo
//
//  Created by 李凯 on 16/9/5.
//  Copyright © 2016年 kevin. All rights reserved.
//

import UIKit
import SDWebImage

class XLWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        let url = URL(string: XLUserAccountViewModel.sharedModel.account?.profileImgURL ?? "")
        iconView.sd_setImage(with: url, placeholderImage: #imageLiteral(resourceName: "avatar_default"))
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //视图显示出来以后, 再做动画
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        //先更新约束
        iconView.snp_updateConstraints { (make) in
            make.top.equalTo(100)
        }
        //设置动画效果
        UIView.animate(withDuration: 2, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0, options: [], animations: {
            self.view.layoutIfNeeded()
            }) { (_) in
                print("指定动画完成")
                //头像的动画完成,再去执行label的动画
                UIView.animate(withDuration: 1, animations: { 
                    self.messageLabel.alpha = 1
                    }, completion: { (_) in
                        NotificationCenter.default.post(name: NSNotification.Name(XLChangeRootVCNotification), object: nil)
                })
        }
        
    }
 
    
    func setupUI() {
        
        self.view.backgroundColor = UIColor(white: 237/255, alpha: 1)
        
        view.addSubview(iconView)
        view.addSubview(messageLabel)
        //布局
        iconView.snp_makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 90, height: 90))
            make.top.equalTo(200)
            make.centerX.equalTo(self.view)
        }
        messageLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(15)
        }
        
    }
    

   //懒加载控件
    lazy var iconView: UIImageView = {
        let v = UIImageView(image: #imageLiteral(resourceName: "avatar_default"))
        //设置圆角
        v.layer.cornerRadius = 45
        v.layer.masksToBounds = true
        //设置边线
        v.layer.borderColor = UIColor.black.cgColor
        v.layer.borderWidth = 1
        return v
    }()
    
    lazy var messageLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGray, fontSize: 16)
        label.alpha = 0
        label.text = "欢迎回来"
        return label
    }()

}
